﻿/*
 * Copyright (c) 2012, Will Strohl
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this 
 * list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice, this 
 * list of conditions and the following disclaimer in the documentation and/or other 
 * materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 * 
*/

using System;
using DotNetNuke.Common;
using DotNetNuke.Entities.Icons;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using WillStrohl.Modules.BookmarkCollector.Components;

namespace WillStrohl.Modules.BookmarkCollector
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View class displays the content
    /// 
    /// Typically your view control would be used to display content or functionality in your module.
    /// 
    /// View may be the only control you have in your project depending on the complexity of your module
    /// 
    /// Because the control inherits from BookmarkCollectorModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : BookmarkCollectorModuleBase, IActionable
    {

        #region Private Members

        private string editIconPath;

        #endregion

        #region Properties

        /// <summary>
        /// Returns the edit icon from the core Icon API
        /// </summary>
        protected string EditIconPath
        {
            get
            {
                // check to see if we already have an icon path to return
                if (string.IsNullOrEmpty(editIconPath))
                {
                    // add the appropriate HTTP/HTTPS
                    editIconPath = (Request.IsSecureConnection) ? "https://" : "http://";
                    // call the icon API and append the domain name / site alias
                    editIconPath = string.Concat(editIconPath, 
                        PortalSettings.PortalAlias.HTTPAlias,
                        IconController.IconURL("Edit", "16x16"));
                }

                // return the icon path
                return editIconPath;
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Called whenever the module loads
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                // only execute the module logic on fresh page loads
                if (!Page.IsPostBack) BindData();
            }
            catch (Exception exc) // Module failed to load
            {
                // save the error to the event log
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        #region Helper Methods

        /// <summary>
        /// Executes the data retrival and binding logic for the module
        /// </summary>
        private void BindData()
        {
            // get a reference to the module business controller class
            var ctlModule = new BookmarkController();
            // ask for a collection of the bookmarks for this module
            var bookmarks = ctlModule.GetContentItems(ModuleId);

            // check to see if any bookmarks were returned
            if (bookmarks != null && bookmarks.Count > 0)
            {
                // bind the bookmarks to the repeater
                rptBookmarks.DataSource = bookmarks;
                rptBookmarks.DataBind();

                // reassign visibility on the page to show the bookmarks view
                pnlBookmarks.Visible = true;
                pnlMessage.Visible = false;
            }
            else
            {
                // show the empty view
                pnlBookmarks.Visible = false;
                pnlMessage.Visible = true;
            }
        }

        /// <summary>
        /// Returns a valid edit URL path to send to the view that edits the bookmark record
        /// </summary>
        /// <param name="bookmarkId"></param>
        /// <returns></returns>
        protected string EditBookmarkUrl(object bookmarkId)
        {
            // this is still an object, so make sure it is not null
            if (bookmarkId != null)
            {
                // return an edit path using the core navigation method
                return EditUrl(string.Empty, string.Empty, "Edit", string.Concat("BookmarkId=", bookmarkId));
            }
            else
            {
                // return a placeholder
                return "#";
            }
        }

        #endregion

        #region Implementations

        /// <summary>
        /// Populates the Manage menu in the module with custom navigation options
        /// </summary>
        public ModuleActionCollection ModuleActions
        {
            get
            {
                // gets a reference to a new module action collection
                var actions = new ModuleActionCollection
                    {
                        // adds a new navigation item to the collection
                        {
                            GetNextActionID(), Localization.GetString("EditModule", LocalResourceFile), string.Empty, string.Empty, string.Empty,
                            EditUrl(), false, SecurityAccessLevel.Edit, true, false
                        }
                    };

                // return these new items to the calling menu logic
                return actions;
            }
        }

        #endregion

    }
}